package edu.lambdateacher.awt;

/*
 LambdaTeacher
 Copyright (C) 2000  Kim Mason
 Copyright (C) 2014  Vitalij Zadneprovskij

 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; either version 2
 of the License, or (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

import java.awt.Button;
import java.awt.Dialog;
import java.awt.Frame;
import java.awt.Panel;
import java.awt.ScrollPane;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import edu.lambdateacher.expressions.Expression;

public class ExpressionChooser extends Dialog {

	private static final long serialVersionUID = 6100413543707332275L;
	
	// the input expression
	private Expression inexpr = null;
	// the chosen expression
	private Expression outexpr = null;

	// an OK button, and its event handler
	private Button okbutton = null;
	private ActionListener okbuttonactionlistener = new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			outexpr = thechooser.getExpr();
			if (outexpr != null) {
				ExpressionChooser.this.setVisible(false);
			}
		}
	};

	// a key event handler
	KeyAdapter keylistener = new KeyAdapter() {
		public void keyPressed(KeyEvent e) {
			switch (e.getKeyCode()) {
			case KeyEvent.VK_ENTER:
				outexpr = thechooser.getExpr();
				if (outexpr != null) {
					ExpressionChooser.this.setVisible(false);
				}
				break;
			case KeyEvent.VK_ESCAPE:
				// simply set the output to null, and close the dialog
				outexpr = null;
				ExpressionChooser.this.setVisible(false);
				break;
			default:
				break;
			}
		}
	};

	// a cancel button, and its event handler
	private Button cancelbutton = null;
	private ActionListener cancelbuttonactionlistener = new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			// simply set the output to null, and close the dialog
			outexpr = null;
			ExpressionChooser.this.setVisible(false);
		}
	};

	// the component which does the choosing
	private ChooseComponent thechooser = null;
	private ScrollPane thescroller = null;

	/*
	 * A constructor. inexpr is the expression to choose from, and parent is the
	 * parent frame.
	 */
	public ExpressionChooser(Expression inexpr, Frame parent) {
		// create a new modal dialog
		super(parent, "Expression Chooser", true);
		okbutton = new Button("OK");
		okbutton.addActionListener(okbuttonactionlistener);
		okbutton.addKeyListener(keylistener);
		cancelbutton = new Button("Cancel");
		cancelbutton.addActionListener(cancelbuttonactionlistener);
		cancelbutton.addKeyListener(keylistener);
		Panel buttonholder = new Panel();
		buttonholder.add(okbutton);
		buttonholder.add(cancelbutton);
		buttonholder.addKeyListener(keylistener);
		thechooser = new ChooseComponent(inexpr);
		this.addKeyListener(keylistener);
		thechooser.addKeyListener(keylistener);
		thescroller = new ScrollPane();
		thescroller.add(thechooser);

		this.add("Center", thescroller);
		this.add("South", buttonholder);
		this.pack();
		this.setSize(400, 200);
	}

	/*
	 * getExpr() simply returns the selected expression
	 */
	public Expression getExpr() {
		return this.outexpr;
	}
}
